package control.character;

import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.control.CameraControl;
import game.constant.Constants;

public class MyPlayerControl extends MyCharacterControl {
    protected boolean left = false, right = false, up = false, down = false;

    public MyPlayerControl(CollisionShape shape, float stepHeight) {
	super(shape, stepHeight);
    }

    @Override
    public void update(float tpf) {
	//TODO limit the player camera's max and min y to stop him from 360-degree looking
	updateWalkDir();
	resetDirs();

	super.update(tpf);
    }

    protected void updateWalkDir() {
	Camera cam = spatial.getControl(CameraControl.class).getCamera();
	Vector3f camDir = cam.getDirection().clone();
	Vector3f camLeft = cam.getLeft().clone();
	camDir.setY(0f);
        camLeft.setY(0f);
	walkDirection.set(0f, 0f, 0f);
	if (left)  { walkDirection.addLocal(camLeft); }
	if (right) { walkDirection.addLocal(camLeft.negate()); }
	if (up)    { walkDirection.addLocal(camDir); }
	if (down)  { walkDirection.addLocal(camDir.negate()); }

	//scale walkDirection based on movement dir
	if (down)
	    walkDirection.normalizeLocal().multLocal((Float)spatial.getUserData(Constants.CHARACTER_CURRENT_SPEED)/100f*0.6f); //60% speed
	else if (left || right)
	    walkDirection.normalizeLocal().multLocal((Float)spatial.getUserData(Constants.CHARACTER_CURRENT_SPEED)/100f*0.8f); //80% speed
	else
	    walkDirection.normalizeLocal().multLocal((Float)spatial.getUserData(Constants.CHARACTER_CURRENT_SPEED)/100f); //100% speed

	setWalkDirection(walkDirection);
    }

    protected void resetDirs() {
	setLeft(false);
	setRight(false);
	setUp(false);
	setDown(false);
    }

    public void setDown(boolean down) {
	this.down = down;
    }

    public void setLeft(boolean left) {
	this.left = left;
    }

    public void setRight(boolean right) {
	this.right = right;
    }

    public void setUp(boolean up) {
	this.up = up;
    }
}
